Skip to content

Fix NPE on goto to invalid method reference#9210

Open
mbien wants to merge 1 commit intoapache:masterfrom
mbien:goto-invalid-method-reference
Open

Fix NPE on goto to invalid method reference#9210
mbien wants to merge 1 commit intoapache:masterfrom
mbien:goto-invalid-method-reference

Conversation

@mbien
Copy link
Member

@mbien mbien commented Feb 17, 2026

  • check if element is a record before listing components
  • removed reflective method calls and updated logic
  • added regression test

reproducer:

public class GoToInvalidMethodReferenceNPE {
    public static void main(String args[]) {
        ChangeListener listener = GoToInvalidMethodReferenceNPE::invalid;
        //                                                       ^^^^^^^ ctrl click (after parsing)
    }
}

fixes #8735

@mbien mbien added this to the NB30 milestone Feb 17, 2026
@mbien mbien requested a review from lahodaj February 17, 2026 04:49
@mbien mbien added Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) Editor ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) labels Feb 17, 2026
Comment on lines 482 to 483
if (peer.getKind() == ElementKind.RECORD_COMPONENT) {
for (RecordComponentElement component : ((TypeElement)peer.getEnclosingElement()).getRecordComponents()) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this cast ok?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it mostly is OK. And it basically weakly suggests that peer.getEnclosingElement() == enclosing, which should be always true. (I probably would just do enclosing.getEnclosingElement(), though.)

I wonder if it is possible to write a test for this - I can try at some point if needed.

Otherwise - great cleanup, thanks for doing this!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I probably would just do enclosing.getEnclosingElement(), though.)

you mean changing

    for (RecordComponentElement component : ((TypeElement)peer.getEnclosingElement()).getRecordComponents()) {

into

    for (RecordComponentElement component : ((TypeElement)enclosing).getRecordComponents()) {

good idea,

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added regression test and updated the logic so that it breaks out of both loops instead of just the inner loop.

 - check if element is a record before listing components
 - removed reflective method calls and updated logic
 - added regression test
@mbien mbien force-pushed the goto-invalid-method-reference branch from 712cb66 to ce2ac52 Compare February 19, 2026 02:33
@mbien mbien marked this pull request as ready for review February 19, 2026 02:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) Editor Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NPE on goto invalid lambda method reference

2 participants

Comments